package com.sc.sysconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.sysconfig.dao.SysDictMapper;
import com.sc.sysconfig.dto.DictQueryDTO;
import com.sc.sysconfig.entity.SysDict;
import com.sc.sysconfig.service.SysDictService;
import com.sc.sysconfig.vo.OptionVO;
import com.sc.sysconfig.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * sys dict服务impl
 *
 * @author 徐赫阳
 * @date 2021/09/03
 */
@Service
@Component
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {

    /**
     * sys dict类型映射器
     */
    @Autowired
    private SysDictMapper sysDictMapper;

    /**
     * 得到dict类型列表
     *
     * @param queryDTO 查询dto
     * @return {@link PageVO}<{@link SysDict}>
     */
    @Override
    public PageVO<SysDict> getDictList(DictQueryDTO queryDTO){
        QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(SysDict::getIsDel,0)
                .isNull(SysDict::getParent)
                .like(queryDTO.getName() != null,SysDict::getName,queryDTO.getName())
                .orderByAsc(SysDict::getId);
        Integer count = this.sysDictMapper.selectList(queryWrapper).size();
        List<SysDict> list = this.sysDictMapper.selectList(queryWrapper.lambda().last("LIMIT "+ (queryDTO.getPage() - 1) * queryDTO.getPageSize() + ","+ queryDTO.getPageSize()));
        for(SysDict menu:list){
            QueryWrapper<SysDict> queryWrapperSub = new QueryWrapper<>();
            queryWrapperSub.lambda()
                    .eq(SysDict::getParent,menu.getId())
                    .orderByAsc(SysDict::getDictValue);
            menu.setChildren(this.sysDictMapper.selectList(queryWrapperSub));
        }
        return PageVO.setPage(queryDTO.getPage(),queryDTO.getPageSize(),list,count);
    }

    /**
     * 得到dict类型选择
     *
     * @return {@link List}<{@link OptionVO}>
     */
    @Override
    public List<OptionVO> getDictOption(){
        return this.sysDictMapper.selectDictOption();
    }

    /**
     * 得到东西
     *
     * @param code 代码
     * @return {@link List}<{@link OptionVO}>
     */
    @Override
    public List<OptionVO> getDict(String code){
        return this.sysDictMapper.selectDict(code);
    }
}
